package com.xpec.c4.game.login.db;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;

import redis.clients.jedis.Jedis;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.ms.service.db.jedis.JedisManager;
import com.ms.service.module.account.Account;
import com.xpec.c4.db.hibernate.manager.HibernateManager;
import com.xpec.c4.db.model.PlayerModel;
import com.xpec.c4.game.utility.GameUtility;
import com.xpec.c4.net.XSENetNettyServer;
import com.xpec.c4.service.module.ModuleName;
import com.xpec.c4.service.module.gateway.CGatewayCodeRequest;

public class LoginDAO {
	public static final String REDIS_GATEWAY_CODE_HEAD = "GATEWAY_CODE";
	
	public static String checkRepeatPlayerName(String name , List<String> nameNumberList){
		String checkStringList = "";
		Session session = HibernateManager.txBegin();
//		SQLQuery query = session.createSQLQuery("select GROUP_CONCAT(num) from PlayerModel where name = :name and nameNumber in(:strList)");
//		Query query = session.createQuery("select GROUP_CONCAT(num) from PlayerModel where name = :name and nameNumber in(:strList)");
		Query query = session.createQuery("from PlayerModel where name = :name and nameNumber in(:strList)");
		query.setParameter("name", name);
		query.setParameterList("strList", nameNumberList);
//		checkStringList = (String)query.uniqueResult();

		ArrayList<PlayerModel> list = (ArrayList<PlayerModel>)query.list();
		int i;
		for(i = 0 ; i < list.size() ; i++){
			checkStringList += list.get(i).getNameNumber();
			if(i < (list.size() - 1)){
				checkStringList += ",";
			}
		}
		HibernateManager.txCommitFailRollbackClearSession();
		return checkStringList;
	}

	/**
	 * 將 gateway code 資訊寫入 redis
	 * 
	 * @param account
	 *            account id
	 * @param code
	 *            gateway code
	 * @param time
	 *            建立時間
	 */
	public static void saveGatewayCodeToRedis(Account account, String code, long time, String agentData) {
		// gateway code
		CGatewayCodeRequest gc = new CGatewayCodeRequest();
		gc.account = account.getAccountId();
		gc.code = code;
		gc.time = time;
		gc.robot = account.isRobot();
		gc.agentData = agentData;

		// 轉 json
		Gson gson = new GsonBuilder().create();
		String json = gson.toJson(gc);

		// redis key
		String key = REDIS_GATEWAY_CODE_HEAD + ":" + code;

		// 有效時間 (秒)
		int duration = XSENetNettyServer.DELAYMAXTIME / 1000;

		// 存
		Jedis jedis = JedisManager.getResource(ModuleName.LOGIN);
		jedis.setex(key, duration, json);
		JedisManager.returnResource(ModuleName.LOGIN, jedis);
	}
	
	/**
	 * 從 redis 中將 gateway code 資訊取出
	 * 
	 * @param code
	 *            gateway code
	 * @return
	 */
	public static CGatewayCodeRequest getGatewayCodeFromRedis(String code) {
		// redis key
		String key = REDIS_GATEWAY_CODE_HEAD + ":" + code;

		// get gateway code
		Jedis jedis = JedisManager.getResource(ModuleName.LOGIN);
		String json = jedis.get(key);
		jedis.del(key);
		JedisManager.returnResource(ModuleName.LOGIN, jedis);

		if (!GameUtility.isHaveString(json)) {
			return null;
		}

		Gson gson = new GsonBuilder().create();
		CGatewayCodeRequest gc = gson.fromJson(json, CGatewayCodeRequest.class);

		return gc;
	}
}
